/*
 * Copyright 2020 edenrun.cn All right reserved. This software is the
 * confidential and proprietary information ofedenrun.cn ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with edenrun.cn.
 */

package com.zoom.pluto.smart.service.impl;

import com.zoom.pluto.common.model.dto.SmartDTO;
import com.zoom.pluto.smart.service.SmartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 骨密度数据访问服务实现
 *
 * @author zoom
 * @date 2020-07-06
 */
@Slf4j
@Service
public class SmartServiceImpl implements SmartService {

    @Override
    public SmartDTO getLatestSmartInfo(String idNo) {
        try {
            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                String url = "jdbc:odbc:smart";
                Connection con = DriverManager.getConnection(url, "admin", "bmtech99");
                Statement sta = con.createStatement();
                ResultSet rst = sta.executeQuery("select * from Measures where PatientsID = \'" + idNo + "\' ");

                if(rst.next()) {
                    return getResult(rst);
                }
                return null;
            } catch(Exception e) {
                log.error("获取骨密度数据异常，用户身份证号：「{}」", idNo, e);
                return null;
            }
        } catch(Exception e) {
            log.error("获取骨密度信息异常");
            return null;
        }
    }

    /**
     * 解析数据库对象
     *
     * @param rst access数据库结果
     * @return 心血管数据
     */
    private SmartDTO getResult(ResultSet rst) {
        SmartDTO smartDTO = new SmartDTO();
        try {
            smartDTO.setMeasuresDate(rst.getDate("MeasuresDate"));
            smartDTO.setMeasuresTime(rst.getDate("MeasuresTime"));
            smartDTO.setPatientsID(getString(rst, "PatientsID"));
            smartDTO.setBUA(rst.getBigDecimal("BUA"));
            smartDTO.setSOS(rst.getBigDecimal("SOS"));
            smartDTO.setOI(rst.getBigDecimal("OI"));
            smartDTO.setTscore(rst.getBigDecimal("Tscore"));
            smartDTO.setZscore(rst.getBigDecimal("Zscore"));
            smartDTO.setTemper(rst.getBigDecimal("Temper"));
            smartDTO.setGPI(rst.getBigDecimal("GPI"));
            smartDTO.setBMI(rst.getBigDecimal("BMI"));
            smartDTO.setMPH(rst.getBigDecimal("MPH"));
            smartDTO.setBoneAge(rst.getBigDecimal("BoneAge"));
            smartDTO.setClassify(rst.getInt("Classify"));
            smartDTO.setBMP_FileName(getString(rst, "BMP_FileName"));
            smartDTO.setHeight(rst.getBigDecimal("Height"));
            smartDTO.setWeight(rst.getBigDecimal("Weight"));
            smartDTO.setADH(rst.getBigDecimal("ADH"));
        } catch(Exception e) {
            log.error("获取信息数据库信息异常", e);
        }
        return smartDTO;
    }

    /**
     * 获取结果集中的字符结果
     *
     * @param rst 结果集
     * @param key 结果Key
     * @return 字符结果
     */
    private String getString(ResultSet rst, String key) {
        try {
            byte[] resultBytes = rst.getBytes(key);
            if(null == resultBytes) {
                return "";
            }
            return new String(resultBytes, "gbk");
        } catch(Exception e) {
            log.error("获取结果集字符异常", e);
            return "";
        }
    }

}
